//
//  BL_MomentCell.m
//  Byelone
//
//  Created by Qi Liu on 2021/2/21.
//

#import "BL_MomentCell.h"
#import "BLUserTagView.h"
#import "BLPhotoContainView.h"
#import "BLVideoView.h"
#import "UIButton+Extension.h"
@interface BL_MomentCell()

@property (weak, nonatomic) IBOutlet UIView *baseView;
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *nickName;
@property (weak, nonatomic) IBOutlet UILabel *height;
@property (weak, nonatomic) IBOutlet UILabel *weight;

@property (weak, nonatomic) IBOutlet UIView *viewTags;
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property (weak, nonatomic) IBOutlet UIView *viewShadow;


@property (weak, nonatomic) IBOutlet BLPhotoContainView *albumView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *albumViewH;

@property (weak, nonatomic) IBOutlet BLVideoView *videoView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *videoVIewH;

@property (weak, nonatomic) IBOutlet UIButton *commnetBtn;
@property (weak, nonatomic) IBOutlet UIImageView *pupImage;

@property (weak, nonatomic) IBOutlet UIButton *likeBtn;

@end

@implementation BL_MomentCell


-(void)showPup:(BOOL)show{
    self.pupImage.hidden = show;
}


-(void)setCommentModel:(BLMomentModel *)commentModel{

    
    _commentModel = commentModel;
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:commentModel.userHomeBaseVo.picture]];
    self.nickName.text = commentModel.userHomeBaseVo.nickname;
    self.height.text = [NSString stringWithFormat:@"%@cm",commentModel.userHomeBaseVo.height];
    self.weight.text = [NSString stringWithFormat:@"%@kg",commentModel.userHomeBaseVo.weight];
    self.contentLabel.text = commentModel.momentContent;
    
    [self.likeBtn setImage:[UIImage imageNamed:@"no_praise"] forState:UIControlStateNormal];
    [self showPup:YES];
    
    NSInteger like = [commentModel.like integerValue];
    if (like == 1) {
        [self.likeBtn setImage:[UIImage imageNamed:@"praise"] forState:UIControlStateNormal];
    }
    [self.likeBtn setTitle:commentModel.likeNum forState:UIControlStateNormal];
    [self.likeBtn layoutButtonWithImageStyle:ZJButtonImageStyleRight imageTitleToSpace:8];
    
    [self.albumView removeAll];
    [self.videoView removeAll];
    

    if ([commentModel.momentType integerValue] == 0) {//纯文字
        self.videoVIewH.constant = 0;
        self.albumViewH.constant = 0;
    }else if ([commentModel.momentType integerValue] == 1){//文字+图片
        self.albumViewH.constant = [self.albumView getHightWIthArray:commentModel.momentUrlList];
        self.videoVIewH.constant = 0;
    }else if([commentModel.momentType integerValue] == 2){//文字+视频
        self.albumViewH.constant = 0;
        self.videoVIewH.constant = [self.videoView getVideoViewHWith:[commentModel.momentUrlList firstObject]];
        MJWeakSelf;
        self.videoView.playBlock = ^{
            if (weakSelf.playBlock) {
                weakSelf.playBlock(weakSelf.commentModel,weakSelf.curIndex);
            }
        };
    }
    
    [self.viewTags.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    NSArray *arrayDesc = @[@{@"title":commentModel.userHomeBaseVo.age,@"img":[UIImage imageNamed:commentModel.userHomeBaseVo.gender.intValue==1?@"common_boy":@"common_girl"],@"color":commentModel.userHomeBaseVo.gender.intValue==1?@"#6891F7":@"#FD5680"},@{@"title":[NSString stringWithFormat:@"%@-%@",commentModel.userHomeBaseVo.livingAreaName,commentModel.userHomeBaseVo.livingCityName],@"img":[UIImage imageNamed:@"common_location"],@"color":@"#56BDFF"}];
    CGFloat descLeft = 0;
    for (int i=0; i<arrayDesc.count; i++) {
        NSDictionary *dict = arrayDesc[i];
        UIView *view = [BLCommonTool createTip:dict[@"img"] title:dict[@"title"] color:[UIColor chColorWithHexString:dict[@"color"]] point:CGPointMake(descLeft, 0)];
        [self.viewTags addSubview:view];
        descLeft = descLeft + view.frame.size.width+6;
    }
}

-(void)stopAnimationGif{
    [self.albumView stopAllGifAnimating];
}
-(void)startAnimationGif{
    [self.albumView startAllGifAnimating];
}

- (IBAction)commnetClick:(id)sender {
    if (_commentBlock) {
        _commentBlock(_commentModel,_curIndex);
    }
}


-(void)hidenPupImage{
    self.pupImage.hidden = YES;
}

- (IBAction)likeClick:(UIButton *)sender {

    if ([_commentModel.like integerValue] == 0) {
        self.pupImage.hidden = NO;
        [self performSelector:@selector(hidenPupImage) withObject:nil afterDelay:1];
    }

    if (_likeBlock) {
        _likeBlock(_commentModel,_curIndex,_likeBtn);
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.iconView.layer.cornerRadius = 15;
    self.iconView.layer.masksToBounds = YES;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.viewShadow.layer.cornerRadius = 10;
    [self.viewShadow setShadowRadius:6 color:BL_COLOR_PURPLE_L(0.12) offset:CGSizeMake(0, 0) opacity:1];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (IBAction)deleteMoment:(UIButton *)sender {
    if (_deleteBlock) {
        _deleteBlock(_commentModel,_curIndex);
    }
}

@end
